import { useRoute } from "vue-router";
import { UseUIStore } from "../store/UI";
import request from '@/utils/request.js'
import { Guid } from "../utils";

export default function () {
    const $route = useRoute();
    const UIStore = UseUIStore();
    const path = $route.path;
    const LoadingCache = new Map();

    /**
     * 隐藏 loading 方法
     *
     * @param {*} id
     * @memberof Request
     */
    const hideLoading = function (id) {
        LoadingCache.delete(id);
        if (LoadingCache.size == 0) {
            UIStore.SetLoadingState(path, false);
        }
    }

    const Event = function (options) {
        const id = Guid();
        if (options?.config?.loading != false) {
            LoadingCache.set(id, '');
            UIStore.SetLoadingState(path, true);
        }
        // 测试用代码
        // return new Promise((resolve, reject) => {
        //     setTimeout(() => {
        //         resolve('success');
        //     }, 10000)
        // }).finally(res => {
        //     hideLoading(id);
        // })
        return request(options).finally(res => {
            hideLoading(id);
        })
    };


    /** @type {Object} http 请求对象 */
    const http = {
        /**
         * api get 接口调用参数
         *
         * @param {String} url  链接地址
         * @param {Object} query Url 传参对象 
         * @param {Object} config 选项对象
         * @return {axios} 接口异步对象
         * 
         * @example 
         *  http.post('/url/{id}', { id : 'text', params : 'Time' }, { loading: false } )
         *  实际请求 URL /url/text?params=Time
         *  实际请求 Body 
         */
        get(url, query, config = { loading: true }) {
            const _data = {};
            _data.type = 'get';
            _data.params = query;
            _data.url = url;
            _data.config = config;
            return Event(_data)
        },

        /**
         * api post 接口调用参数
         *
         * @param {String} url  链接地址
         * @param {Object} query Url 传参对象
         * @param {Object} body body 传参对象 
         * @param {Object} config 选项对象
         * @return {axios} 接口异步对象
         * 
         * @example 
         *  http.post('/url/{id}', { id : 'text', params : 'Time' }, { key : 'value' }, { loading: false } )
         *  实际请求 URL /url/text?params=Time
         *  实际请求 Body { key : 'value' }
         */
        post(url, query, body, config = { loading: true }) {
            const _data = {};
            _data.type = 'post';
            _data.data = body || {};
            _data.params = query;
            _data.url = url;
            _data.config = config;
            return Event(_data)
        },
        /**
         * api post 接口调用参数
         *
         * @param {String} url  链接地址
         * @param {Object} query Url 传参对象
         * @param {Object} body body 传参对象 
         * @param {Object} config 选项对象
         * @return {axios} 接口异步对象
         * 
         * @example 
         *  http.post('/url/{id}', { id : 'text', params : 'Time' }, { key : 'value' }, { loading: false } )
         *  实际请求 URL /url/text?params=Time
         *  实际请求 Body { key : 'value' }
         */
        put(url, query, body, config = { loading: true }) {
            const _data = {};
            _data.type = 'put';
            _data.data = body || {};
            _data.params = query;
            _data.url = url;
            _data.config = config;
            return Event(_data)
        },
        /**
         * api post 接口调用参数
         *
         * @param {String} url  链接地址
         * @param {Object} query Url 传参对象
         * @param {Object} body body 传参对象 
         * @param {Object} config 选项对象
         * @return {axios} 接口异步对象
         * 
         * @example 
         *  http.post('/url/{id}', { id : 'text', params : 'Time' }, { key : 'value' }, { loading: false } )
         *  实际请求 URL /url/text?params=Time
         *  实际请求 Body { key : 'value' }
         */
        delete(url, query, body, config = { loading: true }) {
            const _data = {};
            _data.type = 'delete';
            _data.data = body || {};
            _data.params = query;
            _data.url = url;
            _data.config = config;
            return Event(_data)
        },

        /**
         * 测试接口，用来模拟异步操作,1秒后返回成功状态，自动携带loading
         *
         * @return {Promise} 
         */
        text(url, query, body, config = { loading: true }) {

            const options = {};
            options.type = 'delete';
            options.data = body || {};
            options.params = query;
            options.url = url;
            options.config = config;

            const id = Guid();
            if (options?.config?.loading != false) {
                LoadingCache.set(id, '');
                UIStore.SetLoadingState(path, true);
            }
            return new Promise((resolve) => {
                setTimeout(() => {
                    resolve();
                }, 1000)
            }).finally(res => {
                hideLoading(id);
            });
        }
    }

    return http

}